package com.sergenis.miniapp.jpanel_imagenfondo;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;

import javax.accessibility.Accessible;
import javax.accessibility.AccessibleContext;
import javax.swing.JComponent;

/**
 * Componente para mostrar una imagen en pantalla.
 * <p>
 * Para utilizar el {@code autofit} es importante que se defina
 * {@code preferredSize} por si este se desabilita.
 * </p>
 * 
 * @author Ser Genis
 */
public class ImageBox extends JComponent implements Accessible {
	private static final long	serialVersionUID	= -5753568172653632945L;
	
	public ImageBox() {
		this(null);
	}
	
	public ImageBox(Image image) {
		this.image = image;
	}
	
	private Image				image;
	private ImageScale			imageScale			= ImageScale.MOSAIC;
	private boolean				autofit				= false;
	/**
	 * The <code>AccessibleContext</code> associated with this
	 * <code>JComponent</code>.
	 */
	protected AccessibleContext	accessibleContext	= null;
	
	public Image getImage() {
		return image;
	}
	
	public void setImage(Image image) {
		Image oldValue = this.image;
		this.image = image;
		firePropertyChange("image", oldValue, this.image);
		analyzerAutofit();
	}
	
	public ImageScale getImageScale() {
		return imageScale;
	}
	
	public void setImageScale(ImageScale imageScale) {
		ImageScale oldValue = this.imageScale;
		this.imageScale = imageScale;
		firePropertyChange("imageScale", oldValue, this.imageScale);
	}
	
	public boolean isAutofit() {
		return autofit;
	}
	
	public void setAutofit(boolean autofit) {
		boolean oldValue = this.autofit;
		this.autofit = autofit;
		firePropertyChange("autofit", oldValue, this.autofit);
		analyzerAutofit();
	}
	
	private void analyzerAutofit() {
		if (autofit && image!=null) {
			int w = image.getWidth(this);
			int h = image.getHeight(this);
			setSize(w, h);
		} else setSize(getPreferredSize());
	}
	
	@Override
	public void paint(Graphics graphics) {
		if (!isVisible()) return;
		if (image == null) return;
		
		if (isAutofit()) {
			graphics.drawImage(image, 0, 0, this);
			return;
		}
		
		Graphics2D g = (Graphics2D) graphics.create();
		int outWidth = getWidth();
		int outHeigth = getHeight();
		BufferedImage imageBG = imageScale.getScaleImage(image, outWidth,
				outHeigth);
		int inWidth = imageBG.getWidth(); // Ancho de la imagen
		int inHeight = imageBG.getHeight(); // Alto alto de la imagen
		
		int locX, locY;
		
		switch (imageScale) {
		case FILL: { // Centrando horizontalmente.
			locY = 0;
			locX = (outWidth / 2) - (inWidth / 2);
		}
			break;
		case SET: { // Centrando verticalmente.
			locX = 0;
			locY = (outHeigth / 2) - (inHeight / 2);
		}
			break;
		case CENTRE: { // Centrado vertical y horizontalmente
			locX = (outWidth / 2) - (inWidth / 2);
			locY = (outHeigth / 2) - (inHeight / 2);
		}
			break;
		case EXPAND:
		case MOSAIC:
		default:
			// Se estable la misma imagen sin escalar pero se define que esta
			// inicio en (0, 0)
			locX = 0;
			locY = 0;
			break;
		}
		g.drawImage(imageBG, locX, locY, this);
		
	}
	
}
